package promote.brozen.base.juc;

import promote.brozen.utils.concurrent.Threads;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Brozen
 * @date 2020/8/12 10:35 AM
 * @email brozen@qq.com
 */
public class ReentrantLockAndCondition {

    public static void main(String[] args) throws Exception {

        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t1 in");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("t1 out");
                lock.unlock();
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            Threads.silentSleep(100L);

            lock.lock();
            try {
                System.out.println("t2 in");
                condition.signal();
            } finally {
                System.out.println("t2 out");
                lock.unlock();
            }
        });
        t2.start();

        t1.join();
        t2.join();
    }

}
